import datetime
import time


def get_timestamp_iso_ms(offset=0) -> str:
    '''
    :return:2020-12-08T09:08:57.715Z
    '''
    '''注意：OKEx使用UTC+0时间'''
    iso_time = (datetime.datetime.now() + datetime.timedelta(hours=offset)).isoformat()[:-3] + "Z"
    return iso_time


def get_timestamp_unix() -> str:
    '''
    :return: 精确秒 1621502404
    '''
    timestamp_unix = int(time.time())
    return timestamp_unix


def get_timestamp_unix_ms() -> str:
    '''
    :return: 精确到毫秒 1621502404.688
    '''
    timestamp_unix = str(int(round(time.time(), 3) * 1000))
    return timestamp_unix


def get_timestamp(offset_date=0) -> str:
    '''
    :return: 精确到秒 20210520172004
    '''

    time = datetime.datetime.now()
    time = time + datetime.timedelta(days=int(offset_date))
    timestamp = time.strftime('%Y%m%d%H%M%S%f')[:14]
    return timestamp


def get_date(offset=0) -> str:
    date = get_timestamp(offset_date=offset)[:8]
    return date


def get_timestamp_ms() -> str:
    '''
    :return: 精确到毫秒 20210520172004688
    '''
    timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')[:17]
    return timestamp


def get_timestamp_human() -> str:
    '''
    str:2020-12-11 19:05:46
    '''
    t1 = time.localtime(time.time())
    t2 = time.strftime("%Y-%m-%d %H:%M:%S", t1)
    return t2


def get_date_human() -> str:
    '''
    str:2020-12-11 19:05:46
    '''
    t1 = time.localtime(time.time())
    t2 = time.strftime("%Y-%m-%d", t1)
    return t2


def timestamp_unix_to_timestamp(time_stamp_unix) -> str:
    time_stamp_unix = int(str(time_stamp_unix)[:10])
    t1 = time.localtime(time_stamp_unix)
    timestamp = time.strftime("%Y%m%d%H%M%S", t1)
    return timestamp


def timestamp_unix_to_timestamp_human(time_stamp_human) -> str:
    t1 = time.localtime(float(time_stamp_human))
    time_stamp_human = time.strftime("%Y-%m-%d %H:%M:%S", t1)
    return time_stamp_human


def timestamp_to_timestamp_unix(date_string, date_format):
    # date_string = "2024-08-30"
    # date_format = "%Y-%m-%d"
    date_object = datetime.datetime.strptime(date_string, date_format)
    timestamp_unix = date_object.timestamp()
    timestamp_unix_str = str(int(round(timestamp_unix, 0)))
    return timestamp_unix_str


def timestamp_to_timestamp_unix_ms(date_string, date_format):
    # date_string = "2024-08-30"
    # date_format = "%Y-%m-%d"
    date_object = datetime.datetime.strptime(date_string, date_format)
    timestamp_unix = date_object.timestamp()
    timestamp_unix_str = str(int(round(timestamp_unix, 3) * 1000))
    return timestamp_unix_str


'''定时执行某任务需要单独拆分出一个线程，单线程实现过于繁琐
或者使用缓存设置超时时间'''

# def is_top_of_seconds(n):
#     # 精度为0.01秒
#     current_timestamp = int(get_timestamp())
#     if current_timestamp % n == 0:
#         return True
#     else:
#         return False
#
#
# def is_top_of_minutes(n, precision=0.01):
#     # 精度为0.01秒
#     current_timestamp = int(float(get_timestamp_unix_ms()) * (1 / precision))
#     if current_timestamp % (n * (1 / precision) * 60) == 0:
#         time.sleep(precision)
#         return True
#     else:
#         return False
#
#
# def is_top_of_hours(n, precision=0.01):
#     # 精度为0.01秒
#     current_timestamp = int(float(get_timestamp_unix_ms()) * (1 / precision))
#     if current_timestamp % (n * (1 / precision) * 60 * 60) == 0:
#         time.sleep(precision)
#         return True
#     else:
#         return False


if __name__ == "__main__":
    def test_timestamp_to_timestamp_unix():
        date_string = "2024-08-30"
        date_format = "%Y-%m-%d"
        res = timestamp_to_timestamp_unix(date_string=date_string, date_format=date_format)
        print(res)
        print(type(res))


    def test_timestamp_to_timestamp_unix_ms():
        date_string = "2024-08-30"
        date_format = "%Y-%m-%d"
        res = timestamp_to_timestamp_unix_ms(date_string=date_string, date_format=date_format)
        print(res)
        print(type(res))


    test_timestamp_to_timestamp_unix_ms()
